/**
 * 自定义相机事件
 */
const CustomCameraInput = React.createClass({
    displayName:'CustomCameraInput',
    /**
     * 组件状态
     * @returns {{currentHeading: number, currentCameraPosition: null, cameraDistance: number, viewCenterCarto: Cesium.Cartographic}}
     */
    getInitialState(){
        /**
         * 组件状态
         * @type {object}
         * @property {object} currentHeading 相机当前朝向
         * @property {object} currentCameraPosition 相机当前位置
         * @property {object} cameraDistance 相机视线与地表交点的距离
         * @property {object} viewCenterCarto 相机视线与地表交点的经纬度
         */
        return {
            currentHeading:0,
            currentCameraPosition:null,
            cameraDistance:0,
            viewCenterCarto:new Cesium.Cartographic()
        }
    },

    componentDidMount() {
        if (this.props.isMobile) return;
        const {viewer, homeView} = this.props;
        //右键添加tiltEventTypes事件 实现右键旋转。注意ScreenSpaceEventType、Cesium.CameraEventType的使用与区别。
        //第一条语句把地球旋转功能赋给右键、同时禁止掉滚轮按下旋转功能。第二条语句赋值滚轮放大缩小功能的同时禁止掉右键放大缩小功能。
        viewer.scene.screenSpaceCameraController.tiltEventTypes = Cesium.CameraEventType.RIGHT_DRAG;
        viewer.scene.screenSpaceCameraController.zoomEventTypes = Cesium.CameraEventType.WHEEL;
        //ESC键盘事件。
        document.onkeydown = (e) => {
            let keycode = e.which;
            if (keycode === 27) {
                viewer.camera.flyTo(homeView);
            }
        };
    },
    componentWillReceiveProps(nextProps){
        //2D 3D切换
        const {sceneMode,viewer} = this.props;
        const scene = viewer.scene;
        const camera = viewer.scene.camera;
        const windowPositionSearch = new Cesium.Cartesian2();
        if(nextProps.sceneMode!==sceneMode){
            if(scene.mode===3||scene.mode===2){
                let windowPosition = windowPositionSearch;
                windowPosition.x = scene.canvas.clientWidth / 2;
                windowPosition.y = scene.canvas.clientHeight  / 2;

                let viewCenter = new Cesium.Cartesian3();
                let carto = new Cesium.Cartographic();
                let cameraDistance = 0;
                if(scene.mode===3){
                    let ray = camera.getPickRay(windowPosition);
                    viewCenter = scene.globe.pick(ray,scene,new Cesium.Cartesian3());
                    carto = Cesium.Cartographic.fromCartesian(viewCenter);
                    cameraDistance = Cesium.Cartesian3.distance(camera.position,viewCenter);
                }
                else if(scene.mode===2){
                    viewCenter = camera.pickEllipsoid(windowPosition);
                    carto = Cesium.Cartographic.fromCartesian(viewCenter);
                    cameraDistance = camera.positionCartographic.height;
                }


                //两点之间的距离计算
                // console.log(camera.positionCesium.Cartographic.height);
                // let nextCameraCarto = new Cesium.Cartographic(carto.longitude,carto.latitude,cameraDistance);

                this.setState({
                    currentHeading:camera.heading,
                    viewCenterCarto:carto,
                    cameraDistance:cameraDistance
                });
            }
        }
    },
    componentDidUpdate(prevProps){
        const {sceneMode,viewer} = this.props;
        if(prevProps.sceneMode!==sceneMode){
            const camera = viewer.scene.camera;
            let {viewCenterCarto,currentHeading,cameraDistance} = this.state;
            // console.log(this.state);
            if(sceneMode===2){
                viewer.scene.morphTo2D(0.0);
                camera.lookAt(Cesium.Ellipsoid.WGS84.cartographicToCartesian(viewCenterCarto),new Cesium.HeadingPitchRange(currentHeading, Cesium.Math.toRadians(-90), cameraDistance*1.5));
                camera.setView({
                    orientation : {
                        heading : currentHeading
                    }
                });
                camera.lookAtTransform(Cesium.Matrix4.IDENTITY);
            }
            else if(sceneMode===3){
                viewer.scene.morphTo3D(0.0);
                cameraDistance = cameraDistance/1.5;
                camera.lookAt(Cesium.Ellipsoid.WGS84.cartographicToCartesian(viewCenterCarto),new Cesium.HeadingPitchRange(currentHeading, Cesium.Math.toRadians(-40), cameraDistance));
                camera.lookAtTransform(Cesium.Matrix4.IDENTITY);
            }
        }
    },

    componentWillUnmount() {
        const {viewer} = this.props;
        viewer.scene.screenSpaceCameraController.tiltEventTypes = Cesium.CameraEventType.WHEEL;
        viewer.scene.screenSpaceCameraController.zoomEventTypes = Cesium.CameraEventType.RIGHT_DRAG;
    },
    render(){
        return null;
    }
});